package com.example.sql_2_code_demo.service.impl;

import com.example.sql_2_code_demo.entity.OrderCombineTransaction;
import com.example.sql_2_code_demo.dao.OrderCombineTransactionDao;
import com.example.sql_2_code_demo.service.OrderCombineTransactionService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 用于订单合并支付，多个订单id排序后计算得出的hash(OrderCombineTransaction)表服务实现类
 *
 * @author makejava
 * @since 2022-10-09 10:01:36
 */
@Service("orderCombineTransactionService")
public class OrderCombineTransactionServiceImpl implements OrderCombineTransactionService {
    @Resource
    private OrderCombineTransactionDao orderCombineTransactionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OrderCombineTransaction queryById(Long id) {
        return this.orderCombineTransactionDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param orderCombineTransaction 筛选条件
     * @param pageRequest             分页对象
     * @return 查询结果
     */
    @Override
    public Page<OrderCombineTransaction> queryByPage(OrderCombineTransaction orderCombineTransaction, PageRequest pageRequest) {
        long total = this.orderCombineTransactionDao.count(orderCombineTransaction);
        return new PageImpl<>(this.orderCombineTransactionDao.queryAllByLimit(orderCombineTransaction, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param orderCombineTransaction 实例对象
     * @return 实例对象
     */
    @Override
    public OrderCombineTransaction insert(OrderCombineTransaction orderCombineTransaction) {
        this.orderCombineTransactionDao.insert(orderCombineTransaction);
        return orderCombineTransaction;
    }

    /**
     * 修改数据
     *
     * @param orderCombineTransaction 实例对象
     * @return 实例对象
     */
    @Override
    public OrderCombineTransaction update(OrderCombineTransaction orderCombineTransaction) {
        this.orderCombineTransactionDao.update(orderCombineTransaction);
        return this.queryById(orderCombineTransaction.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.orderCombineTransactionDao.deleteById(id) > 0;
    }
}
